﻿using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Infrasturcture.DapperLight
{
    public class DapperFactoryOptions
    {
        public List<Action<DapperLightOption>> DapperActions = new List<Action<DapperLightOption>>();
    }

    public class DapperFactory : IDapperFactory
    {
        private readonly IOptionsMonitor<DapperFactoryOptions> _option;
        public DapperFactory(IOptionsMonitor<DapperFactoryOptions> options)
        {
            _option = options;
        }
        public DapperClient CreateClient(string name)
        {
            var client = new DapperClient(new DapperLightOption { });

            var clientOption = _option.Get(name).DapperActions.FirstOrDefault();
            if (clientOption == null) throw new ArgumentNullException(nameof(clientOption));

            clientOption(client.DapperOption);

            return client;
        }
    }
}
